package com.miracle.main.module.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.util.Utils;
import com.miracle.main.MainConstants;
import com.miracle.main.R;
import com.miracle.main.R2;
import com.miracle.main.module.mvp.contract.UpdateInfoContract;
import com.miracle.main.module.mvp.presenter.UpdateInfoPresenter;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.miracle.main.MainConstants.UPDATE_TYPE.TYPE_MAIL;
import static com.miracle.main.MainConstants.UPDATE_TYPE.TYPE_NAME;
import static com.miracle.main.MainConstants.UPDATE_TYPE.TYPE_PWD;
import static com.miracle.main.MainConstants.UPDATE_TYPE.TYPE_TEL;

/**
 * com.miracle.main.module.mvp.ui.activity
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/10 11:55
 * @see com.miracle.main.module.mvp.ui.activity
 */
public class UpdateInfoActivity extends BaseActivity<UpdateInfoPresenter> implements
        UpdateInfoContract.View {

    @BindView(R2.id.tv_title)
    TextView mTvTitle;

    @BindView(R2.id.ll_nickname)
    LinearLayout mLLName;
    @BindView(R2.id.ll_pwd)
    LinearLayout mLLPwd;
    @BindView(R2.id.ll_tel)
    LinearLayout mLLTel;
    @BindView(R2.id.ll_mail)
    LinearLayout mLLMail;

    @BindView(R2.id.et_name)
    EditText mEtName;
    @BindView(R2.id.et_opwd)
    EditText mEtOPwd;
    @BindView(R2.id.et_npwd)
    EditText mEtNPwd;
    @BindView(R2.id.et_rpwd)
    EditText mEtRPwd;
    @BindView(R2.id.et_tel)
    EditText mEtTel;
    @BindView(R2.id.et_mail)
    EditText mEtMail;


    /**
     * 类型
     */
    int mUpdateType;

    /**
     * 启动修改界面
     * @param type 类型 {@link com.miracle.main.MainConstants.UPDATE_TYPE}
     */
    public static void start(BaseActivity context, int type) {
        Intent intent = new Intent(context, UpdateInfoActivity.class);
        intent.putExtra(MainConstants.IntentKey.KEY_TYPE, type);
        context.showActivity(intent);
    }

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_activity_update;
    }

    @Override
    public void initView() {
        mUpdateType = getIntent().getIntExtra(MainConstants.IntentKey.KEY_TYPE, TYPE_NAME);
        showViewByType(mUpdateType);
    }

    /**
     * 根据需求显示view
     * @param mUpdateType
     */
    private void showViewByType(int mUpdateType) {
        switch (mUpdateType) {
            case TYPE_NAME:
                mTvTitle.setText("修改昵称");
                mLLName.setVisibility(View.VISIBLE);
                break;
            case TYPE_PWD:
                mTvTitle.setText("修改密码");
                mLLPwd.setVisibility(View.VISIBLE);
                break;
            case TYPE_MAIL:
                mTvTitle.setText("修改邮箱");
                mLLMail.setVisibility(View.VISIBLE);
                break;
            case TYPE_TEL:
                mTvTitle.setText("修改电话");
                mLLTel.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {

    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {

    }

    @Override
    public void showData(Object o) {

    }

    @OnClick({R2.id.iv_back, R2.id.tv_submit})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            onBackPressed();
        }
        if (id == R.id.tv_submit) {
            submitData();
        }
    }

    /**
     * 提交数据
     */
    private void submitData() {
        //昵称
        String name = mEtName.getText().toString().trim();
        //密码
        String oPwd = mEtOPwd.getText().toString().trim();
        String nPwd = mEtNPwd.getText().toString().trim();
        String rPwd = mEtRPwd.getText().toString().trim();
        //电话
        String tel = mEtTel.getText().toString().trim();
        //邮箱
        String mail = mEtMail.getText().toString().trim();

        ArrayList<String> mNamesList = new ArrayList<>();
        ArrayList<String> mErrorList = new ArrayList<>();

        switch (mUpdateType) {
            case TYPE_NAME:
                mNamesList.add(name);
                mErrorList.add("昵称");
                break;
            case TYPE_PWD:
                mNamesList.add(oPwd);
                mErrorList.add("原密码");
                mNamesList.add(nPwd);
                mErrorList.add("新密码");
                mNamesList.add(rPwd);
                mErrorList.add("确认密码");
                break;
            case TYPE_TEL:
                mNamesList.add(tel);
                mErrorList.add("电话");
                break;
            case TYPE_MAIL:
                mNamesList.add(mail);
                mErrorList.add("邮箱");
                break;
            default:
                break;
        }

        if (Utils.checkNullList(mTvTitle, mNamesList, mErrorList)) {
            return;
        }
    }
}
